import qs from 'qs'
function aliyun(option){
    //生成随机文件名，防止重名上传
    var _guid = function(){
        function S4 () {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        }
        return (S4() + S4() + S4() + S4() + S4() + S4());
    };
    // var randomName = _guid();
    var uploader = new plupload.Uploader({
            runtimes : 'html5,flash,silverlight,html4',
            browse_button : option.selectfiles,
            container: document.getElementById(option.container),
            filters:option.fileTypeFilter,
            url : 'https://oss.aliyuncs.com',
            init: {
                PostInit: function() {
                    return false;
                },
                FilesAdded: (up, files)=>{
                    console.log(files)
                    if(files.length>1){
                        alert('一次只能上传一个文件')
                    }
                    let nowfiles = [];
                    files.forEach(item=>{
                        nowfiles.push(item.name);
                    })
                    axios.get('/api/system/getOssConfig?platSign='+option.platSign).then(res=>{
                        res = res.data.data;
                        // randomName = res.Dir + randomName + '.'+files[0].name.split('.')[1];
                        var randomName = res.Dir;
                        uploader.setOption({
                            'url': res.Host,
                            'multipart_params': {
                                'key' : randomName,
                                'path' : randomName, //为了多传时拼接key增加的无用参数，实际不传，详见plupload.dev.js 1407行至1415行，以及89、90行两个参数
                                'filesArr':nowfiles,
                                'policy': res.Policy,
                                'OSSAccessKeyId': res.AccessKeyID,
                                'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
                                'callback' : res.Callback,
                                'signature': res.Signature,
                            }
                        });
                        uploader.start();
                    })
                },

                UploadProgress: function(up, file) {
                    option.getUploadIngFlag(true,file.percent);
                },

                FileUploaded: function(up, file, info) {
                    option.getFileInfo(info.abelKeyArr,file.name);

                    if( typeof option.getCompletionUrl === 'function' || Object.prototype.toString.call(option.getCompletionUrl) === '[object Function]') {

                        axios.post('/api/system/getFullUrl', qs.stringify({
                            fileObject:JSON.stringify([{url:info.abelKeyArr[0],param:""}])
                        })).then((res) => {
                            var d = res.data;
                            if(d.flag == 1){
                                option.getCompletionUrl(d)
                            }else {
                                this.$message.error(d.msg)
                            }
                        })
                    }


                },



                Error: function(up, err) {
                    // console.log('Error')
                },
                Key(ev){
                    console.log(ev)
                }
            }
    });

    return uploader
}

export default aliyun;
